<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="content-type" content="text/html;charset=utf-8">
    <title>iframe API test</title>
</head>
<body>
<script>
    const params = new URLSearchParams(document.location.search);
    const config = JSON.parse(params.get("config"));
    const interfaceConfig = JSON.parse(params.get("interfaceConfig"));
    const password = params.get("password");

    const uInfoObj = params.get("userInfo");
    const userInfo = uInfoObj && uInfoObj.length > 0 ?
        JSON.parse(uInfoObj) : undefined;

    var domain = params.get("domain");
    var options = {
        jwt: params.get("jwt"),
        roomName: params.get("room"),
        configOverwrite: config,
        interfaceConfigOverwrite: interfaceConfig,
        userInfo
    };

    var externalAPIScript = document.createElement('script');
    externalAPIScript.src = `https://${domain}/external_api.js`;
    externalAPIScript.type = "text/javascript";
    externalAPIScript.onload = function(){
        window.jitsiAPI = new JitsiMeetExternalAPI(domain, options);
        // we use this to save data from api to be accessible to tests
        window.jitsiAPI.test = {};
        window.jitsiAPI.addEventListener('participantRoleChanged', function(event) {
            if (event.role === "moderator") {
                window.jitsiAPI.test.isModerator = true;
            }
        });
        window.jitsiAPI.addListener('audioAvailabilityChanged', function(event) {
            window.jitsiAPI.test.audioAvailabilityChanged = event;
        });
        window.jitsiAPI.addListener('videoAvailabilityChanged', function(event) {
            window.jitsiAPI.test.videoAvailabilityChanged = event;
        });
        window.jitsiAPI.addListener('videoConferenceJoined', function(event) {
            window.jitsiAPI.test.videoConferenceJoined = event;
        });
        if (password && password.length > 0) {
            // join a protected channel with the password supplied
            window.jitsiAPI.on('passwordRequired', function ()
            {
                window.jitsiAPI.executeCommand('password', password);
            });
        }
    }
    document.getElementsByTagName('head')[0].appendChild(externalAPIScript);
</script>
</body>
</html>
